<script setup>
import * as api from "@/api/v-sys-service/sys-manage/org/index.js";
import orgFormDialog from "./org-form-dialog.vue"
import {downloadFile} from "@/utils/index.js";
/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

/**
 * 列表数据
 */
const tableData = ref([])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
  formBtnLoading: false,// 保存按钮loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
  showImportExcelDialog: false, //excel导入弹窗
})

/**
 * 弹窗标题
 */
const title = ref("")

const rowData = ref({})

const VTableRef = ref()

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    checkConfig: { highlight: true },
    checkboxConfig: { reserve: true },
    treeConfig: {
      childrenField: 'children',
    },
    showSelectCount: false,
    showExportBtn: false,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  orders: [],
  filters: [],
  condition: {
    key: "",
  },
})

/**
 * 导入选项
 */
const importOptions = ref({
  uploadUrl: '/cloud-sys-service/sys-org/importExcel',
  params: { },//额外参数
  downloadUrl: '/cloud-sys-service/sys-org/downloadTemplate',
  importParamName: 'multipartFile',
  importBtnName: '上传',
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.list(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * 导入
 */
const importExcel = async () => {
  dialog.value.showImportExcelDialog = true
}

const showAddDialog = ($event,row) => {
  // rowData.value = {}
  if (row && row.id) {
    const sort = row.children ? row.children.length + 1 : 1
    rowData.value = {
      parentId: row.id,
      parentName: row.name,
      sort
    }
  }
  else rowData.value = {}
  title.value = "新增组织"
  dialog.value.showFormDialog = true
}
/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = row || {}
  title.value = "编辑组织"
  dialog.value.showFormDialog = true
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.remove(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({
                  message: '删除成功',
                  type: 'success',
                })
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showAddDialog,
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div class="org-box flex flex-col">
    <div class="search flex align-center">
      <label class="label">{{'\u2003'}}关键字{{'\u2003'}}</label>
      <el-input
          v-model="pageQuery.condition.key"
          style="width: 180px;" placeholder="组织名称"
          @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
      />
      <div class="el-button" style="display: none"></div>
      <el-button type="primary" @click="init_PageQuery">
        <template #icon><i-ep-search /></template>搜索
      </el-button>
      <el-button type="success" @click="showAddDialog">
        <template #icon><i-ep-plus /></template>新增
      </el-button>
      <el-button type="primary" @click="importExcel">
        <template #icon><i-ep-upload /></template>导入
      </el-button>
    </div>
    <v-table
        ref="VTableRef"
        v-model="pageQuery"
        :table-info="tableInfo"
        :column-info="api.columnInfo"
        @query="init_PageQuery"
    >
      <template #status="{row}">
        <el-tag v-if="row.status" type="success">启用</el-tag>
        <el-tag v-else type="info">禁用</el-tag>
      </template>
    </v-table>
    <org-form-dialog v-if="dialog.showFormDialog" :title="title" @query="init_PageQuery" v-model="dialog.showFormDialog" :row-data="rowData"/>
    <v-import-excel v-if="dialog.showImportExcelDialog" @query="init_PageQuery" v-model="dialog.showImportExcelDialog" :import-options="importOptions"></v-import-excel>
  </div>
</template>

<style scoped lang="scss">
.org-box {
  padding: 10px;
  .container {
    :deep(.el-button) {
      padding: 0;
    }
  }
}
</style>
